package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.SlainListener;
import net.tv90.bfmccore.battlepass.Battlepass;
import net.tv90.bfmccore.constant.RewardConstant;
import net.tv90.bfmccore.game.EnumGameType;
import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.game.view.GameModelViewHandler;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.util.Ribbon;
import net.tv90.bfmccore.util.TempPermission;
import net.tv90.bfmccore.util.vector.Vector3f;
import org.bitterorange.flansmodslain.actions.Money;
import org.bitterorange.flansmodslain.actions.Point;
import org.bitterorange.flansmodslain.models.BukkitPlayerReviveEvent;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.Objects;

public class ReviveListener implements Listener {

    @EventHandler
    public void playerReviveEvent(BukkitPlayerReviveEvent e) {
        Player reviver = e.getReviver();
        Player revived = e.getRevived();
        if(!Objects.equals(reviver.getName(), revived.getName())) {
            //救援逻辑
            GameManager gm = GameManager.getInstance();
            GameModelViewHandler gmvh = GameModelViewHandler.getInstance();
            if(!GameManager.inBattleFieldPlayers.contains(revived)) {
                revived.spigot().respawn();
                //Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "mv tp " + revived.getName() + " " + gm.currentGame.mapName);
                revived.setHealth(20.0D);
                GameManager.getInstance().currentGame.deploy(revived);
                gm.currentGame.teleportSinglePlayer(revived, new Vector3f((float) e.getPosX(), (float) e.getPosY(), (float) e.getPosZ()));
                revived.playSound(revived.getLocation(), "custom.revive", 100, 1);
                if("Defibrillator".equals(e.getWeapon())) {
                    revived.setHealth(20);
                    revived.setNoDamageTicks(30);
                } else {
                    revived.setHealth(5);
                    revived.setNoDamageTicks(15);
                }
            } else {
                double totalMoneyAdded = 5 * RewardConstant.MONEY_MAGNIFICATION;
                int totalPointsAdded = 2 * RewardConstant.RANK_MAGNIFICATION;
                Money.give(reviver, totalMoneyAdded);
                Point.add(reviver, totalPointsAdded);
                reviver.sendMessage("§e[战场救援]救援失败，该玩家已经重新部署，你获得" + totalMoneyAdded + "游戏币和" + totalPointsAdded + "军衔");
                return;
            }
            //奖励与播报
            Battlepass.addBattlepassPoint(reviver, (int) (100 * RewardConstant.POINT_MAGNIFICATION));
            SlainListener.addLine(revived, "救援者 " + reviver.getName(), 5);
            FactionHelper.getTeamer(reviver).forEach(p -> p.sendMessage("§l§a " + reviver.getName() + " §f[REVIVE] §a" + revived.getName()));
            FactionHelper.getEnemies(reviver).forEach(p -> p.sendMessage("§l§c " + reviver.getName() + " §f[REVIVE] §c" + revived.getName()));
            double totalMoneyAdded = 10 * RewardConstant.MONEY_MAGNIFICATION;
            int totalPointsAdded = 5 * RewardConstant.RANK_MAGNIFICATION;
            Money.give(reviver, totalMoneyAdded);
            Point.add(reviver, totalPointsAdded);
            reviver.sendMessage("§e[战场救援]你获得" + totalMoneyAdded + "游戏币和" + totalPointsAdded + "军衔");
            Ribbon.addNumberForRibbon(reviver, "Revive", "战场救援", 10);

            //小队死斗加分
            if(gm.currentGame.getGameType() == EnumGameType.SDM) {
                if (TempPermission.hasTempPerm(revived, "bfmc.squadleader")) {
                    gm.currentGame.addTicket(revived, 10);
                }
            }
        }
    }
}
